package com.ruoyi;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.vo.ProjectDetail;
import com.ruoyi.vo.ProjectVo;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

//@RestController
public class Test {
    public void t(@RequestParam("file") MultipartFile file) throws IOException {
//        //0.创建RestClient对象
//        //1.创建请求语义对象
//        CreateIndexRequest request = new CreateIndexRequest("test");
//        // 本次创建索引库需要添加属性映射
//        // 参数1: json格式的属性映射
//        // 参数2: 格式类型
//        request.mapping("{\n" +
//                "    \"properties\": {\n" +
//                "      \"info\":{\n" +
//                "        \"type\": \"text\",\n" +
//                "        \"index\": true,\n" +
//                "        \"analyzer\": \"ik_max_word\"\n" +
//                "      },\n" +
//                "      \"email\":{\n" +
//                "        \"type\": \"keyword\",\n" +
//                "        \"index\": false\n" +
//                "      },\n" +
//                "      \"name\":{\n" +
//                "        \"properties\": {\n" +
//                "          \"firstName\":{\n" +
//                "            \"type\": \"text\",\n" +
//                "            \"index\": false\n" +
//                "          },\n" +
//                "          \"lastName\":{\n" +
//                "            \"type\": \"text\",\n" +
//                "            \"index\": false\n" +
//                "          }\n" +
//                "        }\n" +
//                "      }\n" +
//                "    }\n" +
//                "  }", XContentType.JSON);
//        //2.发送请求给ES,并接收响应结果
////        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
//        //3.处理响应结果
//        boolean flag = response.isAcknowledged();
//        if (flag){
//            System.out.println("创建索引库成功...");
//        }
    }

    public static void main(String[] args) {

//        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\lenovo\\Desktop\\资金文件解析表.xlsx");
//        //获取父项目信息
//        ProjectDetail parentProject = getParentProject(reader);
//        //获取所有的子项目
//        List<ProjectDetail> childProjectDetails = getChildProject(reader);
//        System.out.println(childProjectDetails.size());
//        ProjectVo projectVo = new ProjectVo();
//        projectVo.setParentProject(parentProject);
//        projectVo.setChildProjectVos(childProjectDetails);
//        String s = JSONObject.toJSONString(projectVo);
//        System.out.println(s);
            //1.创建请求语义对象
    }

    /**
     * 获取子项目数据集合
     *
     * @param reader
     * @return
     */
    private static List<ProjectDetail> getChildProject(ExcelReader reader) {
        List<ProjectDetail> list = new ArrayList<>();
        //获取行数
        int rowCount = reader.getRowCount();
        //获取列数
        int columnCount = reader.getColumnCount();
        //循环读取单元格值 子项目的信息  特点 2 3 4行 为中央 省级 合计行
        //从第六行起  就是各地区数据
        //列 从第三列开始读取
        for (int i = 2; i < columnCount; i++) {
            Object projectName = reader.readCellValue(i, 1);
            if (null != projectName && StringUtils.isNotEmpty(projectName.toString())) {
                for (int j = 2; j < rowCount; j++) {
                    Object area = reader.readCellValue(0, j + 3);
                    if (area != null && StringUtils.isNotEmpty(area.toString())) {
                        ProjectDetail projectDetail = new ProjectDetail();
                        //获取中央资金
                        Object zy = reader.readCellValue(i, 2);
                        if (zy != null) {
                            projectDetail.setZyZj(zy.toString());
                        }
                        //省级
                        Object sj = reader.readCellValue(i, 3);
                        if (sj != null) {
                            projectDetail.setSjZj(sj.toString());
                        }
                        //合计
                        Object hj = reader.readCellValue(i, 4);
                        if (hj != null) {
                            projectDetail.setHj(hj.toString());
                        }
                        //资金额
                        Object zj = reader.readCellValue(i, j + 3);
                        if (zj != null) {
                            projectDetail.setZj(zj.toString());
                        }
                        projectDetail.setProjectName(projectName.toString());//项目名称
                        projectDetail.setArea(area.toString());
                        System.out.println(projectDetail);
                        list.add(projectDetail);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 获取父项目信息
     */
    public static ProjectDetail getParentProject(ExcelReader reader) {
        ProjectDetail projectDetail = new ProjectDetail();
        //获取父项目名称
        Object projectName = reader.readCellValue(0, 0);
        if (null == projectName) {
            throw new RuntimeException("父项目名称为空，请检查excel文件");
        }
        projectDetail.setProjectName(projectName.toString());
        //获取父项目的中央资金
        Object zyZj = reader.readCellValue(1, 2);
        if (null != zyZj) {
            projectDetail.setZyZj(zyZj.toString());
        }
        //获取父项目的省级资金
        Object sjZj = reader.readCellValue(1, 3);
        if (null != sjZj) {
            projectDetail.setSjZj(sjZj.toString());
        }
        //获取父项目的合计资金
        Object hj = reader.readCellValue(1, 4);
        if (null == hj) {
            throw new RuntimeException("父项目合计金额为空，请检查excel文件");
        }
        projectDetail.setHj(hj.toString());
        return projectDetail;
    }

}
